1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * diOBJECTibuted under the License is diOBJECTibuted on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.base.Equivalence.Wrapper;
21  import com.google.common.collect.ImmutableList;
22  import com.google.common.testing.EqualsTester;
23  import com.google.common.testing.EquivalenceTester;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Unit test for {@link Equivalence}.
29   *
30   * @author Jige Yu
31   */
32  @GwtCompatible(emulated = true)
33  public class EquivalenceTest extends TestCase {
34    @SuppressWarnings("unchecked") // varargs
35    public void testPairwiseEquivalent() {
36      EquivalenceTester.of(Equivalence.equals().<String>pairwise())
37          .addEquivalenceGroup(ImmutableList.<String>of())
38          .addEquivalenceGroup(ImmutableList.of("a"))
39          .addEquivalenceGroup(ImmutableList.of("b"))
40          .addEquivalenceGroup(ImmutableList.of("a", "b"), ImmutableList.of("a", "b"))
41          .test();
42    }
43  
44    public void testPairwiseEquivalent_equals() {
45      new EqualsTester()
46          .addEqualityGroup(Equivalence.equals().pairwise(), Equivalence.equals().pairwise())
47          .addEqualityGroup(Equivalence.identity().pairwise())
48          .testEquals();
49    }
50  
51    private enum LengthFunction implements Function<String, Integer> {
52      INSTANCE;
53  
54      @Override public Integer apply(String input) {
55        return input.length();
56      }
57    }
58  
59    private static final Equivalence<String> LENGTH_EQUIVALENCE = Equivalence.equals()
60        .onResultOf(LengthFunction.INSTANCE);
61  
62    public void testWrap() {
63      new EqualsTester()
64          .addEqualityGroup(
65              LENGTH_EQUIVALENCE.wrap("hello"),
66              LENGTH_EQUIVALENCE.wrap("hello"),
67              LENGTH_EQUIVALENCE.wrap("world"))
68          .addEqualityGroup(
69              LENGTH_EQUIVALENCE.wrap("hi"),
70              LENGTH_EQUIVALENCE.wrap("yo"))
71          .addEqualityGroup(
72              LENGTH_EQUIVALENCE.wrap(null),
73              LENGTH_EQUIVALENCE.wrap(null))
74          .addEqualityGroup(Equivalence.equals().wrap("hello"))
75          .addEqualityGroup(Equivalence.equals().wrap(null))
76          .testEquals();
77    }
78  
79    public void testWrap_get() {
80      String test = "test";
81      Wrapper<String> wrapper = LENGTH_EQUIVALENCE.wrap(test);
82      assertSame(test, wrapper.get());
83    }
84  
85    private static class IntValue {
86      private final int value;
87      
88      IntValue(int value) {
89        this.value = value;
90      }
91  
92      @Override public String toString() {
93        return "value = " + value;
94      }
95    }
96    
97    public void testOnResultOf() {
98      EquivalenceTester.of(Equivalence.equals().onResultOf(Functions.toStringFunction()))
99          .addEquivalenceGroup(new IntValue(1), new IntValue(1))
100         .addEquivalenceGroup(new IntValue(2))
101         .test();
102   }
103   
104   public void testOnResultOf_equals() {
105     new EqualsTester()
106         .addEqualityGroup(
107             Equivalence.identity().onResultOf(Functions.toStringFunction()),
108             Equivalence.identity().onResultOf(Functions.toStringFunction()))
109         .addEqualityGroup(Equivalence.equals().onResultOf(Functions.toStringFunction()))
110         .addEqualityGroup(Equivalence.identity().onResultOf(Functions.identity()))
111         .testEquals();
112   }
113   
114   public void testEquivalentTo() {
115     Predicate<Object> equalTo1 = Equivalence.equals().equivalentTo("1");
116     assertTrue(equalTo1.apply("1"));
117     assertFalse(equalTo1.apply("2"));
118     assertFalse(equalTo1.apply(null));
119     Predicate<Object> isNull = Equivalence.equals().equivalentTo(null);
120     assertFalse(isNull.apply("1"));
121     assertFalse(isNull.apply("2"));
122     assertTrue(isNull.apply(null));
123     
124     new EqualsTester()
125         .addEqualityGroup(equalTo1, Equivalence.equals().equivalentTo("1"))
126         .addEqualityGroup(isNull)
127         .addEqualityGroup(Equivalence.identity().equivalentTo("1"))
128         .testEquals();
129   }
130   public void testEqualsEquivalent() {
131     EquivalenceTester.of(Equivalence.equals())
132         .addEquivalenceGroup(new Integer(42), 42)
133         .addEquivalenceGroup("a")
134         .test();
135   }
136 
137   public void testIdentityEquivalent() {
138     EquivalenceTester.of(Equivalence.identity())
139         .addEquivalenceGroup(new Integer(42))
140         .addEquivalenceGroup(new Integer(42))
141         .addEquivalenceGroup("a")
142         .test();
143   }
144   
145   public void testEquals() {
146     new EqualsTester()
147         .addEqualityGroup(Equivalence.equals(), Equivalence.equals())
148         .addEqualityGroup(Equivalence.identity(), Equivalence.identity())
149         .testEquals();
150   }
151 }
152